home *** CD-ROM | disk | FTP | other *** search
/ Fritz: All Fritz / All Fritz.zip / All Fritz / FILES / PROGMISC / OVL312.LZH / OVL312.ARC / SCULPT.C < prev    next >
Text File  |  1989-05-06  |  15KB  |  627 lines

  1. /* SCULPT.C
  2.  
  3.     programmed in the small memory model of Turbo C version 2.0
  4.  
  5.     Create $$ODATA.OBJ file for use with OVL/PROVL system, version 3.12
  6.     and above.
  7.  
  8. */
  9.  
  10. #include "stdio.h"
  11. #include "dos.h"
  12. #include "conio.h"
  13. #include "ctype.h"
  14. #include "mem.h"
  15.  
  16. #define PUBDEF 0x90
  17. #define LNAMES 0x96
  18. #define SEGDEF 0x98
  19. #define MODEND 0x8a
  20. #define THEADR 0x80
  21. #define LEDATA 0xa0
  22.  
  23. #define BACKSPACE 8
  24. #define RETURN 13
  25. #define INSERT 210
  26. #define DELETE 211
  27. #define CURUP 200
  28. #define CURL 203
  29. #define CURR 205
  30. #define CURDN 208
  31. #define F9 195
  32. #define F10 196
  33. #define ESC 27
  34.  
  35. #define BLANKS(A) (&blanks[80-A])
  36.  
  37. FILE *fpbanner,*foutptr;
  38.  
  39. int input_len,xcur,ycur,textpos;
  40. int funkey=0,btoggle=0;    /* btoggle==0 for text, ==1 for file */
  41. int upperflag=0;
  42. int command_len,path_len,banner_len;
  43.  
  44. char chksum;    /* checksum for object module records */
  45. char textbuff[81];
  46. char recbuff[1032];    /* buffer to hold bytes of record to be written */
  47. char banbuff[4096];    /* buffer to hold characters from banner sign-on file */
  48. char *banner_message[2]={
  49.     "Press <F9> to specify an ASCII file containing the sign-on banner.",
  50.     "Press <F9> to specify one line of text for the sign-on banner.    "
  51. };
  52. char *banner_prompt[2]={
  53.     "Sign-on Banner Text:                    ",
  54.     "Sign-on Banner ASCII File Specification:"
  55. };
  56. char *blanks="                                                                                ";
  57.  
  58. char pathstring[82]={
  59.     "PATH="
  60. };
  61. char bannerstring[81]={
  62.     "Loading...."
  63. };
  64. char bannerfile[81]={
  65.     "SIGN-ON.TXT"
  66. };
  67. char commandstring[83]={
  68.     "/ZDOVERLAY.LOG"
  69. };
  70.  
  71. void show_screen();
  72. void put_up_text(unsigned char comm),insert_text(int x,int y),edit_text(unsigned char command);
  73. void back_char(int x,int y),del_char (int x,int y),beep(),wipe();
  74. void get_input(),save_info(),get_path(),get_banner(),get_command();
  75. void write_modend(),write_lnames(),write_theadr(),write_segdef();
  76. void write_pubdef(),write_ledata(),write_objmod();
  77. void compute_chksum(char *chkbuff,unsigned int numchars);
  78.  
  79. int ginput(int x,int y,char *s,int length),inkey();
  80.  
  81. void main()
  82. {
  83.     show_screen();
  84.     get_input();
  85.     if(funkey==ESC){    /* leave without saving */
  86.         clrscr();
  87.         exit(0);
  88.     }
  89.     else if(funkey==F10){
  90.         wipe();    /* remove prompts */
  91.         save_info();    /* save the information shown */
  92.     }
  93.     clrscr();
  94.     exit(0);
  95. }
  96.  
  97. void show_screen()
  98. {
  99.     clrscr();
  100.     gotoxy(1,2);
  101.     cprintf("Overlay File Path Environment String:\r\n");
  102.     textattr(0x70);
  103.     cprintf(BLANKS(80));
  104.     gotoxy(1,3);
  105.     cprintf("%s",pathstring);
  106.     textattr(7);
  107.     gotoxy(1,6);
  108.     cprintf("%s",banner_prompt[btoggle]);
  109.     textattr(0x70);
  110.     gotoxy(1,7);
  111.     cprintf(BLANKS(80));
  112.     gotoxy(1,7);
  113.     cprintf("%s",bannerstring);
  114.     textattr(7);
  115.     gotoxy(1,10);
  116.     cprintf("Command Line Switch(es):\r\n");
  117.     textattr(0x70);
  118.     cprintf(BLANKS(80));
  119.     gotoxy(1,11);
  120.     cprintf("%s",commandstring);
  121.     textattr(7);
  122.     gotoxy(1,23);
  123.     cprintf("Press <F10> to create Overlay Data Object File and exit.");
  124.     gotoxy(1,24);
  125.     cprintf("Press <ESC> to exit without creating Overlay Data Object File.");
  126. }
  127.  
  128. void wipe()    /* erase function key prompts */
  129. {
  130.     gotoxy(1,21);
  131.     clreol();
  132.     gotoxy(1,23);
  133.     clreol();
  134.     gotoxy(1,24);
  135.     clreol();
  136. }
  137.  
  138. void save_info()
  139. {
  140.     int len;
  141.     char temp[82];
  142.  
  143.     foutptr=fopen("$$ODATA.OBJ","wb");    /* open data file */
  144.     if(foutptr==NULL){    /* problem opening file */
  145.         clrscr();
  146.         gotoxy(1,2);
  147.         cprintf("*** Error opening file $$ODATA.OBJ");
  148.         gotoxy(1,3);
  149.         cprintf("*** $$ODATA.OBJ file NOT created.");
  150.         exit(1);
  151.     }
  152.     len=strlen(pathstring);
  153.     if(len && pathstring[len-1]!='='){    /* make sure path has '=' at end */
  154.         strcat(pathstring,"=");
  155.     }
  156.     strcat(commandstring,"\r");    /* append CR to end of commandstring */
  157.     len=strlen(commandstring);
  158.     strcpy(&temp[1],commandstring);
  159.     temp[0]=len;    /* temp has command string with length prepended */
  160.     strcpy(commandstring,temp);
  161.     write_objmod();
  162.     fclose(foutptr);    /* close data file */
  163.  
  164. }
  165.  
  166. void get_input()
  167. {
  168.     static int pos=0;
  169.  
  170.     while(1){
  171.         switch(pos){
  172.             case 0:
  173.                 get_path();
  174.                 break;
  175.             case 1:
  176.                 get_banner();
  177.                 break;
  178.             case 2:
  179.                 get_command();
  180.                 break;
  181.             default:    /* hey, how'd you get here? */
  182.                 pos=0;    /* force valid position */
  183.                 break;
  184.         }
  185.         if((funkey==F10 || funkey==RETURN) && btoggle && bannerfile[0]){    /* check that file exists */
  186.             if(!(access(bannerfile,0)==0)){    /* file does not exist */
  187.                 beep();
  188.                 gotoxy(1,8);
  189.                 cprintf("File not found.");
  190.                 continue;
  191.             }
  192.         }
  193.         gotoxy(1,8);
  194.         cprintf(BLANKS(16));
  195.         if(funkey==ESC || funkey==F10){
  196.             break;
  197.         }
  198.         else if (funkey==RETURN || funkey==CURDN){
  199.             pos=(pos<2?pos+1:0);
  200.         }
  201.         else if(funkey==CURUP){
  202.             pos=(pos>0?pos-1:2);
  203.         }
  204.     }
  205. }
  206.  
  207. void get_path()
  208. {
  209.     gotoxy(1,21);
  210.     cprintf(BLANKS(80));
  211.     textattr(0x70);
  212.     while(1){
  213.         upperflag=1;    /* force caps */
  214.         funkey=ginput(1,3,pathstring,80);
  215.         upperflag=0;
  216.         if(funkey!=F9)
  217.             break;
  218.         else
  219.             beep();
  220.     }
  221.     textattr(7);
  222. }
  223.  
  224. void get_banner()
  225. {
  226.     gotoxy(1,21);
  227.     cprintf("%s",banner_message[btoggle]);
  228.     textattr(0x70);
  229.     while(1){
  230.         if(!btoggle){
  231.             textattr(7);
  232.             gotoxy(1,8);    /* blank file not found message field */
  233.             cprintf(BLANKS(16));
  234.             textattr(0x70);
  235.             funkey=ginput(1,7,bannerstring,80);
  236.         }
  237.         else{
  238.             funkey=ginput(1,7,bannerfile,80);
  239.         }
  240.         if(funkey!=F9){
  241.             break;
  242.         }
  243.         else{    /* toggle message */
  244.             btoggle^=1;
  245.             textattr(7);
  246.             gotoxy(1,6);
  247.             cprintf("%s",banner_prompt[btoggle]);
  248.             gotoxy(1,21);
  249.             cprintf("%s",banner_message[btoggle]);
  250.             textattr(0x70);
  251.         }
  252.     }
  253.     textattr(7);
  254. }
  255.  
  256. void get_command()
  257. {
  258.     gotoxy(1,21);
  259.     cprintf(BLANKS(80));
  260.     textattr(0x70);
  261.     while(1){
  262.         funkey=ginput(1,11,commandstring,80);
  263.         if(funkey!=F9)
  264.             break;
  265.         else
  266.             beep();
  267.     }
  268.     textattr(7);
  269. }
  270.  
  271. /*    generic input -- pass it x,y location, string to print in s, and length.
  272.     returns new string in s    */
  273. int ginput(int x,int y,char *s,int length)
  274. {
  275.     unsigned char command;
  276.     int i;
  277.  
  278.     input_len=length;
  279.     xcur=x;
  280.     ycur=y;
  281.     textpos=0;
  282.  
  283.     for (i=0;i<input_len;i++)
  284.         textbuff[i]=' ';
  285.     i=0;
  286.     while(s[i]){
  287.         textbuff[i]=s[i];    
  288.         i++;
  289.     }
  290.     textbuff[input_len]=0;
  291.     gotoxy(x,y);
  292.     cprintf("%s",textbuff);
  293.     i=strlen(s);
  294.     if(i==input_len)
  295.         i--;
  296.     textpos=i;
  297.     xcur=x+i;
  298.     gotoxy(x+i,y);
  299.     command=0;
  300.     while (1){
  301.         command=inkey ();
  302.         if (command==RETURN || command==F9 || command==F10 || command==ESC || command==CURUP || command==CURDN)
  303.             break;
  304.         else if (command==CURL || command==CURR)
  305.             edit_text (command);
  306.         else if (command==DELETE)
  307.             del_char (x,y);
  308.         else if (command==INSERT)
  309.             insert_text (x,y);
  310.         else if (command==BACKSPACE)
  311.             back_char(x,y);
  312.         else if(command<' ' || command>'~'){
  313.             beep();
  314.             continue;
  315.         }
  316.         else{
  317.             put_up_text (command);
  318.         }
  319.         gotoxy(xcur,ycur);
  320.     }
  321.     i=strlen(textbuff);
  322.     while(i>=0 && (textbuff[i]<=' ' || textbuff[i]>'~'))
  323.         textbuff[i--]=0;    /* strip trailing whitespace */
  324.     strcpy(s,textbuff);
  325.     return((int)(command));
  326. }    
  327.  
  328. void put_up_text(unsigned char comm)    /* print a character, used by input routines */
  329. {
  330.     gotoxy(xcur,ycur);
  331.     cprintf("%c",comm);
  332.     textbuff[textpos]=comm;
  333.     if(textpos<input_len-1){
  334.         textpos++;
  335.         xcur++;
  336.     }
  337. }
  338.  
  339. void insert_text(int x,int y)    /* insert a character, used by input routines */
  340. {
  341.     char temp[82];
  342.  
  343.     if (textbuff[input_len-1] == ' '){
  344.         strcpy(temp,textbuff);
  345.         strcpy(&temp[textpos+1],&textbuff[textpos]);
  346.         temp[input_len]=0;
  347.         temp[textpos]=' ';
  348.         gotoxy(x,y);
  349.         cprintf("%s",temp);
  350.         strcpy(textbuff,temp);
  351.     }
  352.     else
  353.         beep();
  354. }
  355.  
  356. void edit_text(unsigned char command)    /* move cursor left or right, used by input routines */
  357. {
  358.     if (command==CURR && textpos<input_len-1){
  359.         textpos++;
  360.         xcur++;
  361.     }
  362.     else if (command==CURL && textpos>0){
  363.         textpos--;
  364.         xcur--;
  365.     }
  366.     else if (textpos==0 || textpos==input_len-1)
  367.         beep();
  368. }
  369.  
  370. void back_char(int x,int y)    /* backspace a character, used by input routines */
  371. {
  372.     if(textpos==0)
  373.         beep();
  374.     else if(textpos==input_len-1 && textbuff[textpos]!=' ')
  375.         textbuff[textpos]=' ';
  376.     else{
  377.         textpos--;
  378.         textbuff[textpos]=' ';
  379.         xcur--;
  380.     }
  381.     gotoxy(x,y);
  382.     cprintf("%s",textbuff);
  383. }
  384.  
  385. void del_char(int x,int y)    /* delete a character, used by input routines */
  386. {
  387.     strcpy(&textbuff[textpos],&textbuff[textpos+1]);
  388.     textbuff[input_len-1]=' ';
  389.     textbuff[input_len]=0;
  390.     gotoxy(x,y);
  391.     cprintf("%s",textbuff);
  392. }
  393.  
  394. void beep()
  395. {
  396.     sound(4000);
  397.     delay(200);
  398.     nosound();
  399. }
  400.  
  401. int inkey()
  402. {
  403.     int c;
  404.  
  405.     c=getch();
  406.     if(upperflag){
  407.         c=toupper(c);
  408.     }
  409.     if(!c){
  410.         c=getch();
  411.         c+=128;        /* set high bit to indicate special key */
  412.     }
  413.     return(c);
  414. }
  415.  
  416. void write_objmod()
  417. {
  418.     command_len=strlen(commandstring);
  419.     path_len=strlen(pathstring);
  420.     if(!btoggle){    /* pull sign-on banner from entered text */
  421.         banner_len=strlen(bannerstring);
  422.         strcpy(banbuff,bannerstring);    /* copy string into banner buffer */
  423.     }
  424.     else{    /* pull sign-on banner from file */
  425.         if(bannerfile[0]){
  426.             fpbanner=fopen(bannerfile,"rb");
  427.             banner_len=fread(banbuff,1,4096,fpbanner);
  428.             fclose(fpbanner);
  429.         }
  430.         else{    /* no file name given */
  431.             banner_len=0;
  432.         }
  433.     }
  434.     write_theadr();
  435.     write_lnames();
  436.     write_segdef();
  437.     write_pubdef();
  438.     write_ledata();
  439.     write_modend();
  440. }
  441.  
  442. void write_theadr()
  443. {
  444.     recbuff[0]=THEADR;    /* record type */
  445.     recbuff[1]=10;    /* two byte record length (remaining bytes including checksum byte) */
  446.     recbuff[2]=0;
  447.     recbuff[3]=8;    /* T-module name field */
  448.     recbuff[4]='$';
  449.     recbuff[5]='$';
  450.     recbuff[6]='M';
  451.     recbuff[7]='D';
  452.     recbuff[8]='.';
  453.     recbuff[9]='A';
  454.     recbuff[10]='S';
  455.     recbuff[11]='M';
  456.     compute_chksum(recbuff,12);
  457.     recbuff[12]=chksum;
  458.     fwrite((void *)recbuff,13,1,foutptr);
  459. }
  460.  
  461. void write_lnames()
  462. {
  463.     recbuff[0]=LNAMES;    /* record type */
  464.     recbuff[1]=17;    /* two byte record length */
  465.     recbuff[2]=0;
  466.     recbuff[3]=0;    /* name list */
  467.     recbuff[4]=9;    /* name list */
  468.     recbuff[5]='$';
  469.     recbuff[6]='$';
  470.     recbuff[7]='M';
  471.     recbuff[8]='D';
  472.     recbuff[9]='_';
  473.     recbuff[10]='D';
  474.     recbuff[11]='A';
  475.     recbuff[12]='T';
  476.     recbuff[13]='A';
  477.     recbuff[14]=4;    /* name list */
  478.     recbuff[15]='C';
  479.     recbuff[16]='O';
  480.     recbuff[17]='D';
  481.     recbuff[18]='E';
  482.     compute_chksum(recbuff,19);
  483.     recbuff[19]=chksum;
  484.     fwrite((void *)recbuff,20,1,foutptr);
  485. }
  486.  
  487. void write_segdef()
  488. {
  489.     unsigned int seglen;
  490.  
  491.     recbuff[0]=SEGDEF;    /* record type */
  492.     recbuff[1]=7;    /* two byte record length */
  493.     recbuff[2]=0;
  494.     recbuff[3]=0x60;    /* ACBP byte, relocatable, paragraph aligned, private */
  495.  
  496.     /* get length of data bytes plus banner length bytes */
  497.     seglen=path_len+banner_len+2+command_len;
  498.     movmem(&seglen,&recbuff[4],2);    /* two byte segment length */
  499.  
  500.     recbuff[6]=2;    /* segment name index */
  501.     recbuff[7]=3;    /* class name index */
  502.     recbuff[8]=1;    /* overlay name index */
  503.     compute_chksum(recbuff,9);
  504.     recbuff[9]=chksum;
  505.     fwrite((void *)recbuff,10,1,foutptr);
  506. }
  507.  
  508. void write_pubdef()
  509. {
  510.     int offset;
  511.  
  512.     recbuff[0]=PUBDEF;    /* record type */
  513.     recbuff[1]=59;    /* two byte record length */
  514.     recbuff[2]=0;
  515.     recbuff[3]=0;    /* group index */
  516.     recbuff[4]=1;    /* segment index */
  517.     recbuff[5]=10;    /* name length */
  518.     movmem("$$ovl_path",&recbuff[6],10);    /* name */
  519.     recbuff[16]=0;    /* public offset */
  520.     recbuff[17]=0;
  521.     recbuff[18]=0;    /* type index */
  522.  
  523.     recbuff[19]=14;    /* name length */
  524.     movmem("$$ovl_cmd_line",&recbuff[20],14);    /* name */
  525.     movmem(&path_len,&recbuff[34],2);    /* public offset */
  526.     recbuff[36]=0;    /* type index */
  527.  
  528.     recbuff[37]=20;    /* name length */
  529.     movmem("$$ovl_signon_message",&recbuff[38],20);    /* name */
  530.     offset=command_len+path_len;
  531.     movmem(&offset,&recbuff[58],2);    /* public offset */
  532.     recbuff[60]=0;    /* type index */
  533.  
  534.     compute_chksum(recbuff,61);
  535.     recbuff[61]=chksum;
  536.     fwrite((void *)recbuff,62,1,foutptr);
  537. }
  538.  
  539. void write_ledata()
  540. {
  541.     int reclen,offset,loop,chunk;
  542.  
  543.     if(path_len){    /* write path string */
  544.         recbuff[0]=LEDATA;    /* record type */
  545.         reclen=path_len+4;    /* length of string plus seg index, data offset, checksum bytes */
  546.         movmem(&reclen,&recbuff[1],2);    /* two byte record length */
  547.         recbuff[3]=1;    /* segment index */
  548.         recbuff[4]=0;    /* enumerated data offset */
  549.         recbuff[5]=0;
  550.         movmem(pathstring,&recbuff[6],path_len);    /* bytes of data */
  551.         compute_chksum(recbuff,path_len+6);
  552.         recbuff[path_len+6]=chksum;
  553.         fwrite((void *)recbuff,path_len+7,1,foutptr);
  554.     }
  555.  
  556.     if(command_len){    /* write command line */
  557.         recbuff[0]=LEDATA;    /* record type */
  558.         reclen=command_len+4;    /* length of string plus seg index, data offset, checksum bytes */
  559.         movmem(&reclen,&recbuff[1],2);    /* two byte record length */
  560.         recbuff[3]=1;    /* segment index */
  561.         movmem(&path_len,&recbuff[4],2);    /* enumerated data offset (comes after pathstring) */
  562.         movmem(commandstring,&recbuff[6],command_len);    /* bytes of data */
  563.         compute_chksum(recbuff,command_len+6);
  564.         recbuff[command_len+6]=chksum;
  565.         fwrite((void *)recbuff,command_len+7,1,foutptr);
  566.     }
  567.  
  568.     if(banner_len){    /* write sign-on banner message */
  569.         offset=command_len+path_len;    /* data starts after path and command data strings */
  570.         for(loop=0;loop<banner_len;loop+=512){    /* write data in 512 byte chunks */
  571.             if(loop+512>=banner_len){    /* odd sized chunk */
  572.                 chunk=banner_len-loop;
  573.             }
  574.             else{
  575.                 chunk=512;
  576.             }
  577.             recbuff[0]=LEDATA;    /* record type */
  578.             if(!loop){    /* first time thru */
  579.                 chunk+=2;    /* bump chunk to account for banner data length */
  580.                 reclen=chunk+4;
  581.                 movmem(&reclen,&recbuff[1],2);    /* two byte record length */
  582.                 recbuff[3]=1;    /* segment index */
  583.                 movmem(&offset,&recbuff[4],2);    /* enumerated data offset */
  584.                 movmem(&banner_len,&recbuff[6],2);    /* banner length data */
  585.                 movmem(&banbuff[loop],&recbuff[8],chunk-2);    /* banner data */
  586.                 compute_chksum(recbuff,chunk+6);
  587.                 recbuff[chunk+6]=chksum;
  588.                 fwrite((void *)recbuff,chunk+7,1,foutptr);
  589.                 offset+=514;
  590.             }
  591.             else{
  592.                 reclen=chunk+4;
  593.                 movmem(&reclen,&recbuff[1],2);    /* two byte record length */
  594.                 recbuff[3]=1;    /* segment index */
  595.                 movmem(&offset,&recbuff[4],2);    /* enumerated data offset */
  596.                 movmem(&banbuff[loop],&recbuff[6],chunk);    /* banner data */
  597.                 compute_chksum(recbuff,chunk+6);
  598.                 recbuff[chunk+6]=chksum;
  599.                 fwrite((void *)recbuff,chunk+7,1,foutptr);
  600.                 offset+=512;
  601.             }
  602.         }
  603.     }
  604. }
  605.  
  606. void write_modend()
  607. {
  608.     recbuff[0]=MODEND;    /* record type */
  609.     recbuff[1]=2;    /* two byte record length */
  610.     recbuff[2]=0;
  611.     recbuff[3]=0;    /* module type */
  612.     compute_chksum(recbuff,4);
  613.     recbuff[4]=chksum;
  614.     fwrite((void *)recbuff,5,1,foutptr);
  615. }
  616.  
  617. void compute_chksum(char *chkbuff,unsigned int numchars)
  618. {
  619.     unsigned int i;
  620.     unsigned char total=0;
  621.  
  622.     for(i=0;i<numchars;i++){
  623.         total+=chkbuff[i];
  624.     }
  625.     chksum=~total+1;
  626. }
  627.